package com.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.List;

public class ClassmateListActivity extends AppCompatActivity {

    private ListView text_list;
    private TextView textView;
    private ImageView icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_list);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {


    }

    private void initData() {
        getData();
    }

    private void initView() {
        text_list = findViewById(R.id.list_text);

    }
    private void getData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        String url = " http://api.boruiman.com/app3/classmate/list ";

        JSONObject jsonObject = new JSONObject();



        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject jsonObject) {

                Log.e("TAG", "onResponse: " + jsonObject);

                /*处理结果数据*/

                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）

                Gson gson = new Gson();

                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);

                if(myBean.isSuccess()){

                    Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();

                    // 获取链表数据

                    List<MyBean.ListBean> list = myBean.getList();

                    // 构建适配器对象

                    MyAdapter adapter = new MyAdapter(list,getApplicationContext());

                    // 为listview设置适配器对象

                    text_list.setAdapter(adapter);

                }else{

                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();

                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError volleyError) {

                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();

            }

        });

        requestQueue.add(jsonObjectRequest);

    }



    class MyAdapter extends BaseAdapter {
        private List<MyBean.ListBean> list;
        private Context context;

        public MyAdapter(List<MyBean.ListBean> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_listview, parent, false);
            //这里写listview里面item的控件，findViewById前面要加上(view.)。
            textView = view.findViewById(R.id.tv_name);

            // 把DataBean的数据从List里面取出来。
             MyBean.ListBean listBean = list.get(position);
            // 获取当前项的数据中的某个属性的值，将其设置为文本框控件的属性，完成数据的呈现。
             textView.setText(list.get(position).getName()+"");
                             return view;
            // 返回填充了内容的项视图对象。    }}
        }
    }
}